package com.kzd.btgame.main.home

import android.content.Intent
import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.ActivityBargainDetailsBinding
import com.kzd.btgame.entity.BargainDetails
import com.kzd.btgame.jetpack.vm.BargainViewModel
import com.kzd.btgame.main.*
import com.kzd.btgame.main.home.adapter.BargainImageAdapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel
import java.lang.StringBuilder

class BargainDetailsActivity : OwnerViewBindingActivity<ActivityBargainDetailsBinding>() {

    private val mBargainImageAdapter = BargainImageAdapter()

    private val mBargainId: String
        get() = intent.extras?.getString(KEY_BARGAIN_DETAILS_ID) ?: ""


    private val mViewModel: BargainViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityBargainDetailsBinding
        get() = ActivityBargainDetailsBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.tlBargainDetailsBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.rvBargainDetailsImageBody.adapter = mBargainImageAdapter

        mViewBinding.tvBargainDetailsCollect.setOnClickAntiShakeListener {
            val isCollect = mViewModel.bargainDetailsLiveData.value
                ?.data?.data?.shoucang ?: 0
            if (isCollect == 0) checkLogin {
                mViewModel.userCollectAdd(mBargainId, type = 2)
            } else checkLogin {
                mViewModel.userCollectDel(mBargainId, type = 2)
            }
        }

        mViewBinding.tvGameListDownload.setOnClickAntiShakeListener {
            val gameId = mViewModel.bargainDetailsLiveData.value?.data?.data?.gid
                ?: return@setOnClickAntiShakeListener
            toGameDetailsActivity(gameId)
        }

        mViewBinding.tvBargainDetailsSubmit.setOnClickAntiShakeListener {
            val data = mViewModel.bargainDetailsLiveData.value?.data?.data
                ?: return@setOnClickAntiShakeListener
            if (data.status == "1" && data.shop_ok == "0") {
                toBargainPaymentActivity(data)
            } else if (data.status == "2") {
                toBargainSellActivity(data)
            }
        }
    }

    override fun initData() {
        mViewModel.bargainDetails(mBargainId)
    }

    override fun initViewMode() {
        mViewModel.bargainDetailsLiveData.observe(this as LifecycleOwner, { response ->
            mViewBinding.mslMultiStatusLayout.setLoadingStatus(response)
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })

        mViewModel.userCollectAddLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            mViewModel.bargainDetailsLiveData.value?.data?.data?.apply {
                this.shoucang = 1
            }
            toast(response.data?.message)
        })

        mViewModel.userCollectDelLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            mViewModel.bargainDetailsLiveData.value?.data?.data?.apply {
                this.shoucang = 0
            }
            toast(response.data?.message)
        })
    }

    private fun fillData(bd: BargainDetails) {
        mViewBinding.ivBargainDetailsIcon.loadImage(bd.icon)
        mViewBinding.tvBargainDetailsName.text = bd.gname
        mViewBinding.tvBargainDetailsPrice.text = StringBuilder()
            .append("￥").append(bd.money)
        mViewBinding.tvBargainDetailsType.text = StringBuilder()
            .append(bd.type).append(" · ").append(bd.number)
            .append("次下载 · ").append(bd.size).append("MB")
        mViewBinding.tvBargainDetailsTime.text = bd.create_time
        mViewBinding.tvBargainDetailsSmall.text = bd.role_name
        mViewBinding.tvBargainDetailsService.text = bd.sname
        mViewBinding.tvBargainDetailsStatus.text = StringBuilder()
            .append("此号已创建").append(bd.regday).append("天，实际累充：")
            .append(bd.pay_money).append("元")
        mViewBinding.tvBargainDetailsIntroduction.text = bd.name
        mViewBinding.tvBargainDetailsContent.text = bd.introduction

        mBargainImageAdapter.setNewInstance(bd.images)

        mViewBinding.ivBargainDetailsStatus.apply {
            if (bd.status == "0" && bd.shop_ok == "0") {
                setImageResource(R.mipmap.trade_checking_icon)
            } else if (bd.status == "1" && bd.shop_ok == "0") {
                setImageResource(R.mipmap.trade_checked_icon)
            } else if (bd.status == "1" && bd.shop_ok == "1") {
                setImageResource(R.mipmap.trade_finish_icon)
            } else if (bd.status == "2") {
                setImageResource(R.mipmap.trade_rejected_icon)
            } else {
                setImageResource(0)
            }
        }

        mViewBinding.tvBargainDetailsSubmit.apply {
            text = if (bd.status == "0" && bd.shop_ok == "0") {
                "待审核"
            } else if (bd.status == "1" && bd.shop_ok == "0") {
                "立即购买"
            } else if (bd.status == "1" && bd.shop_ok == "1") {
                "已出售"
            } else if (bd.status == "2") {
                "重新出售"
            } else {
                "立即购买"
            }
        }.isEnabled = if (bd.status == "0" && bd.shop_ok == "0") {
            false
        } else if (bd.status == "1" && bd.shop_ok == "0") {
            true
        } else if (bd.status == "1" && bd.shop_ok == "1") {
            false
        } else if (bd.status == "2") true else true
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        initData()
    }

}